﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using System.Collections;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;

namespace logicunivesityinventorystore.Store
{
    public partial class ViewReport : System.Web.UI.Page
    {
        RequisitionFacade r = new RequisitionFacade();
        protected void Page_Load(object sender, EventArgs e)
        {          
            if (!IsPostBack)
            {
                List<string> months = new List<string>() { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
                ddlMonths.DataSource = months;
                ddlMonths.DataBind();
                List<string> year = new List<string>() { "2012", "2013" };
                ddlYear.DataSource = year;
                ddlYear.DataBind();
                List<string> categories = new List<string>() { "By Category", "By Employee" };
                ddlCategory.DataSource = categories;
                ddlCategory.DataBind();
            }
        }
        public int getMonthsFormat()
        {
            int iMonth = 0;
            switch ((ddlMonths.SelectedValue).ToString())
            {
                case "January":
                    iMonth = 1;
                    break;
                case "February":
                    iMonth = 2;
                    break;
                case "March":
                    iMonth = 3;
                    break;
                case "April":
                    iMonth = 4;
                    break;
                case "May":
                    iMonth = 5;
                    break;
                case "June":
                    iMonth = 6;
                    break;
                case "July":
                    iMonth = 7;
                    break;
                case "August":
                    iMonth = 8;
                    break;
                case "September":
                    iMonth = 9;
                    break;
                case "October":
                    iMonth = 10;
                    break;
                case "November":
                    iMonth = 11;
                    break;
                case "December":
                    iMonth = 12;
                    break;

            }
            return iMonth;
        }
     

        protected void optMonths_CheckedChanged1(object sender, EventArgs e)
        {
            ddlMonths.Enabled = optMonths.Checked;
            ddlYear.Enabled = !optMonths.Checked;
        }

        protected void optYear_CheckedChanged1(object sender, EventArgs e)
        {
            ddlMonths.Enabled = !optYear.Checked;
            ddlYear.Enabled = optYear.Checked;
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            //if(optMonths.Checked==true && ddlCategory.Text =="By Category")
            //{
            //    int months = getMonthsFormat();
            //    GridView1.DataSource = new RequisitionFacade().ViewReportByCategoryInMonth(months);
            //    GridView1.DataBind();
               
            //}
            //if (optYear.Checked == true && ddlCategory.Text == "By Category") 
            //{
            //    int Years = Convert.ToInt32(ddlYear.SelectedValue);
            //    GridView1.DataSource = new RequisitionFacade().ViewReportByCategoryInYear(Years);
            //    GridView1.DataBind();
            //}
            //if (optMonths.Checked == true && ddlCategory.Text == "By Employee")
            //{
            //    int months = getMonthsFormat();
            //    GridView1.DataSource = new RequisitionFacade().ViewReportByEmployeeInMonth(months);
            //    GridView1.DataBind();
            
            //}
            //if (optMonths.Checked == true && ddlCategory.Text == "By Employee")
            //{
            //    int year = Convert.ToInt32(ddlYear.SelectedValue);
            //    GridView1.DataSource = new RequisitionFacade().ViewReportByEmployeeInYear(year);
            //    GridView1.DataBind();

            //}
        }
      

        protected void btnPrint_Click1(object sender, EventArgs e)
        {

            string printScript =
          @"function PrintReport()
         {
            var toPrint = document.getElementById('print');
            var printWindow = window.open('gview.htm','PrintWindow','letf=0,top=0,width=500,height=300,toolbar=1,scrollbars=1,status=1');
            printWindow.document.write(toPrint.innerHTML);
            printWindow.document.close();
            printWindow.focus();
            printWindow.print();
            printWindow.close();}";
            this.ClientScript.RegisterStartupScript(Page.GetType(), "PrintReport", printScript.ToString(), true);
            btnPrint.Attributes.Add("onclick", "PrintReport();");
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            ReportDocument rptDoc = new ReportDocument();
            ReportDocument rptDoc1 = new ReportDocument();
            ViewReportDataSet ds = new ViewReportDataSet(); // .xsd file name
            DataTable dt = new DataTable();
            dt.TableName = "Crystal Report Example";
            dt = getAllOrders(); //This function is located below this function
            ds.Tables[0].Merge(dt);
            rptDoc.Load(Server.MapPath("../Store/ViewReportByCategory.rpt"));
            rptDoc.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = rptDoc;

        }
        public DataTable getAllOrders()
        {                     
            string sqlCon = "Data Source=10.10.2.93;Initial Catalog=InventoryStoreDB;Password=gaday123@;User ID=gaday";
            SqlConnection Con = new SqlConnection(sqlCon);
            SqlParameter splParam = new SqlParameter("@months", getMonthsFormat());
            SqlCommand cmd = new SqlCommand();
            DataSet ds = null;
            SqlDataAdapter adapter;
            try
            {
                Con.Open();
                cmd.CommandText = "viewReportByCategoryInMonthProc";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(splParam);
                //cmd.Parameters.Add(splParams);
                cmd.Connection = Con;
                ds = new DataSet();
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "ReorderReport");

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                if (Con.State != ConnectionState.Closed)
                    Con.Close();
            }
            return ds.Tables[0];
        }
    }
}